'use strict';
/**
 * Created by FreezeSoul on 2015/10/13.
 */
define([
        'angular',
        'app/modules/schema/define',
        'app/modules/schema/services/schema-tree',
        'app/modules/dataset/services/dataset',
        'app/modules/schema/directives/unique-code',
        'app/modules/dataset/controllers/dataset-delete'
    ],
    function (angular) {
        angular.module('ad.schema')
            .controller('adSchemaEditController', ['$rootScope', '$scope', '$filter', '$state', '$stateParams', '$uibModal', '_', 'Notification', 'adSchemaTreeService', 'adDataSetService'
                , function ($rootScope, $scope, $filter, $state, $stateParams, $uibModal, _, Notification, schemaService, dataService) {

                    $scope.$stateParams = $stateParams;

                    $scope.schemaDataTypes = adConfig.schemaDataTypes;
                    $scope.schemaAnalysisTypes = adConfig.schemaAnalysisTypes;

                    $scope.columns = [];

                    $scope.datasets = [];

                    $scope.loadSchema = function () {
                        schemaService.loadSchema($scope.$stateParams.schemaid).then(function (data) {
                            var obj = angular.fromJson(data);
                            if (obj.result == 'success') {
                                $scope.item = obj.data;
                                var jsondata = obj.data.schema_data;
                                if (jsondata) {
                                    $scope.columns = angular.fromJson(jsondata);
                                }
                            } else {
                                Notification.error('数据模型加载失败！');
                            }
                        });
                    };

                    $scope.loadDataset = function () {
                        dataService.loadDatasetBySchema($scope.$stateParams.schemaid).then(function (data) {
                            var obj = angular.fromJson(data);
                            if (obj.result == 'success') {
                                $scope.datasets = obj.data;
                            } else {
                                Notification.error('数据集加载失败！');
                            }
                        });
                    };

                    $scope.showDataType = function (item) {
                        var selected = [];
                        if (item.DataType) {
                            selected = $filter('filter')($scope.schemaDataTypes, {value: item.DataType});
                        }
                        return selected.length ? selected[0].text : '未设置';
                    };

                    $scope.showAnalysisType = function (item) {
                        var selected = [];
                        if (item.AnalysisType) {
                            selected = $filter('filter')($scope.schemaAnalysisTypes, {value: item.AnalysisType});
                        }
                        return selected.length ? selected[0].text : '未设置';
                    };

                    $scope.changeDataType = function (item, type) {
                        if (type == "int") {
                            item.AnalysisType = "measure";
                        } else if (type == "string" || type == "time") {
                            item.AnalysisType = "dimension";
                        }
                    };

                    $scope.removeColumn = function (index) {
                        $scope.columns[index].isDeleted = true;
                        //$scope.columns.splice(index, 1);
                        $scope.schema_Form.$setDirty()
                    };

                    $scope.addColumn = function () {
                        $scope.inserted = {
                            Code: '',
                            Name: '',
                            DataType: null,
                            AnalysisType: null,
                            isDeleted: false,
                            isNew: true
                        };
                        $scope.columns.push($scope.inserted);
                        $scope.schema_Form.$setDirty()
                    };

                    $scope.cancelColumn = function () {
                        for (var i = $scope.columns.length; i--;) {
                            var column = $scope.columns[i];
                            if (column.isDeleted) {
                                delete column.isDeleted;
                            }
                            if (column.isNew) {
                                $scope.columns.splice(i, 1);
                            }
                        }
                        ;
                    };

                    $scope.filterColumn = function (column) {
                        return column.isDeleted !== true;
                    };

                    $scope.save = function () {
                        var cloneColumns = _.filter($scope.columns, function (column) {
                            return column.isDeleted !== true &&
                                column.Code !== "";
                        });

                        _.each(cloneColumns, function (column) {
                            delete column.isDeleted;
                            delete column.isNew;
                        });

                        $scope.item.schema_data = angular.toJson(cloneColumns);

                        schemaService.updateSchema($scope.item).then(function (data) {
                            var obj = angular.fromJson(data);
                            if (obj.result == 'success') {
                                Notification.success('保存成功！');
                                $state.reload();
                            }
                        });
                    };

                    $scope.gotoDefault = function () {
                        $state.go("editor.schema.default")
                    };

                    $scope.editDataset = function (schema, dataset) {
                        $rootScope.$broadcast('ad.message.pathinfo', '数据集管理');
                        $state.go('editor.schema.dataset-edit', {
                            schemaid: schema.schema_id,
                            datasetid: dataset.dataset_id
                        });
                    };

                    $scope.deleteDataset = function (dataset) {
                        var modalInstance = $uibModal.open({
                            animation: true,
                            templateUrl: 'app/modules/dataset/views/dataset-delete.tpl.html',
                            controller: 'adDataSetDeleteController',
                            size: 'sm',
                            resolve: {
                                items: function () {
                                    return [dataset];
                                }
                            }
                        });

                        modalInstance.result.then(function (data) {
                            var obj = angular.fromJson(data);
                            if (obj.result == 'success') {
                                Notification.success('删除成功！');
                            }
                            $state.reload();
                        }, function () {
                        });
                    };

                    $scope.addDataset = function (schema) {
                        $rootScope.$broadcast('ad.message.pathinfo', '数据集管理');
                        $state.go('editor.schema.dataset-edit', {schemaid: schema.schema_id, datasetid: 0});
                    };

                    $scope.loadSchema();
                    $scope.loadDataset();
                }]);
    });